/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the lkyh.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.lkyh.mes.codegen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkyh.mes.codegen.entity.GenDataView;
import com.lkyh.mes.codegen.entity.GenDatasourceConf;
import com.lkyh.mes.codegen.mapper.GenDataViewMapper;
import com.lkyh.mes.codegen.service.GenDataViewService;
import com.lkyh.mes.codegen.service.GenDatasourceConfService;
import com.lkyh.mes.codegen.util.FormUtils;
import lombok.AllArgsConstructor;
import ognl.DefaultMemberAccess;
import ognl.OgnlContext;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 数据指标
 *
 * @author mes code generator
 * @date 2020-05-18 00:30:50
 */
@Service
@AllArgsConstructor
public class GenDataViewServiceImpl extends ServiceImpl<GenDataViewMapper, GenDataView> implements GenDataViewService {

	private final GenDatasourceConfService genDatasourceConfService;
	private final StringEncryptor stringEncryptor;

	@Override
	public List<Map<String, Object>> run(GenDataView genDataView) {

		String params = genDataView.getParams();
		GenDatasourceConf datasourceConf = genDatasourceConfService.getById(genDataView.getDatasourceId());
		DriverManagerDataSource dds = new DriverManagerDataSource();
		dds.setUrl(datasourceConf.getUrl());
		dds.setDriverClassName(datasourceConf.getDriverClassName());
		dds.setUsername(datasourceConf.getUsername());
		dds.setPassword(stringEncryptor.decrypt(datasourceConf.getPassword()));

		String[] args = new String[0];
		if (StrUtil.isNotBlank(params)) {
			args = params.split(StrUtil.COMMA);
		}
		int count = StrUtil.count(genDataView.getSql(), "?");
		//校验sql,前台不会带参,把参数置换成不会匹配到的值执行
		if (genDataView.isVerify() && count > 0 && args.length == 0) {
			args = new String[count];
			for (int i = 0; i < count; i++) {
				args[i] = StrUtil.toString(System.currentTimeMillis());
			}
		}
		OgnlContext context = new OgnlContext(null, null, new DefaultMemberAccess(true));
		return new JdbcTemplate(dds).queryForList(genDataView.getSql(),
				Stream.of(args).filter(StrUtil::isNotBlank).map(i -> FormUtils.getVal(i, context)).toArray());
	}
}
